// MailStatDlg.cpp : implementation file
//

#include "stdafx.h"
#include "MailStat.h"
#include "MailStatDlg.h"
#include "AboutDlg.h"
#include "AppTools.h"
#include "PECore.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CMailStatDlg dialog

HWND g_hMainWnd = 0;
extern int g_iStopStat;


CMailStatDlg::CMailStatDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMailStatDlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

	m_hDialogBrush = CreateSolidBrush(DLG_BK_COLOR);
	m_hTitleBarBrush = CreateSolidBrush(RGB(255,0,0));
}

void CMailStatDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_PROGRESS_STAT, m_StatProgress);
	DDX_Control(pDX, IDC_FROM_DATE, m_StartTimePicker);
	DDX_Control(pDX, IDC_TO_DATE, m_EndTimePicker);
}

BEGIN_MESSAGE_MAP(CMailStatDlg, CDialog)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
	ON_BN_CLICKED(ID_BTN_ABOUT, &CMailStatDlg::OnBnClickedBtnAbout)
	ON_BN_CLICKED(IDC_BUTTON_CLOSE, &CMailStatDlg::OnBnClickedButtonClose)
	ON_WM_CTLCOLOR()
	ON_WM_NCHITTEST()
	ON_BN_CLICKED(IDC_BUTTON_MIN, &CMailStatDlg::OnBnClickedButtonMin)
	ON_BN_CLICKED(IDC_CHECK_TIME_RANGE, &CMailStatDlg::OnBnClickedCheckTimeRange)
	ON_BN_CLICKED(IDC_BTN_START_STAT, &CMailStatDlg::OnBnClickedBtnStartStat)
	ON_BN_CLICKED(IDC_RADIO_WHOLE, &CMailStatDlg::OnBnClickedRadioWhole)
	ON_BN_CLICKED(IDC_RADIO_THIS_MONTH, &CMailStatDlg::OnBnClickedRadioThisMonth)
	ON_BN_CLICKED(IDC_RADIO_THIS_WEEK, &CMailStatDlg::OnBnClickedRadioThisWeek)
	ON_BN_CLICKED(IDC_RADIO_TODAY, &CMailStatDlg::OnBnClickedRadioToday)
	ON_BN_CLICKED(IDC_BTN_EXPORT, &CMailStatDlg::OnBnClickedBtnExport)
END_MESSAGE_MAP()


// CMailStatDlg message handlers

BOOL CMailStatDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

	m_btnClose.SubclassDlgItem(IDC_BUTTON_CLOSE, this);
	m_btnMin.SubclassDlgItem(IDC_BUTTON_MIN, this);

	m_btnClose.DrawTransparent();
	m_btnMin.DrawTransparent();

	m_btnClose.SetIcon(IDI_CLOSE_WIN);
	m_btnMin.SetIcon(IDI_MIN_WIN);

	m_btnAbout.SubclassDlgItem(ID_BTN_ABOUT, this);
	m_btnAbout.SetColor(CButtonST::BTNST_COLOR_BK_OUT,SUB_BTN1_COLOR);
	m_btnAbout.SetColor(CButtonST::BTNST_COLOR_BK_IN,SUB_BTN1_SEL_COLOR);
	m_btnAbout.SetColor(CButtonST::BTNST_COLOR_BK_FOCUS,SUB_BTN1_COLOR);
	m_btnAbout.SetColor(CButtonST::BTNST_COLOR_FG_IN,SUB_BTN1_FONT_FOCUS_COLOR);
	m_btnAbout.SetColor(CButtonST::BTNST_COLOR_FG_OUT,SUB_BTN1_FONT_COLOR);
	m_btnAbout.SetColor(CButtonST::BTNST_COLOR_FG_FOCUS,SUB_BTN1_FONT_COLOR);

	m_btnStart.SubclassDlgItem(IDC_BTN_START_STAT, this);
	m_btnStart.SetColor(CButtonST::BTNST_COLOR_BK_OUT,SUB_BTN1_COLOR);
	m_btnStart.SetColor(CButtonST::BTNST_COLOR_BK_IN,SUB_BTN1_SEL_COLOR);
	m_btnStart.SetColor(CButtonST::BTNST_COLOR_BK_FOCUS,SUB_BTN1_COLOR);
	m_btnStart.SetColor(CButtonST::BTNST_COLOR_FG_IN,SUB_BTN1_FONT_FOCUS_COLOR);
	m_btnStart.SetColor(CButtonST::BTNST_COLOR_FG_OUT,SUB_BTN1_FONT_COLOR);
	m_btnStart.SetColor(CButtonST::BTNST_COLOR_FG_FOCUS,SUB_BTN1_FONT_COLOR);

	m_btnExport.SubclassDlgItem(IDC_BTN_EXPORT, this);
	m_btnExport.SetColor(CButtonST::BTNST_COLOR_BK_OUT,SUB_BTN1_COLOR);
	m_btnExport.SetColor(CButtonST::BTNST_COLOR_BK_IN,SUB_BTN1_SEL_COLOR);
	m_btnExport.SetColor(CButtonST::BTNST_COLOR_BK_FOCUS,SUB_BTN1_COLOR);
	m_btnExport.SetColor(CButtonST::BTNST_COLOR_FG_IN,SUB_BTN1_FONT_FOCUS_COLOR);
	m_btnExport.SetColor(CButtonST::BTNST_COLOR_FG_OUT,SUB_BTN1_FONT_COLOR);
	m_btnExport.SetColor(CButtonST::BTNST_COLOR_FG_FOCUS,SUB_BTN1_FONT_COLOR);

	m_StatProgress.SetRange(0,100);

	SetWindowText(_T("MailStats"));

	OnBnClickedCheckTimeRange();

	g_hMainWnd = m_hWnd;

	m_pUIContainer = new CUIContainer();
	m_pUIContainer->Create(CUIContainer::IDD);
	//m_pUIContainer->Go2Black();

	Init();

	m_pUIContainer->AdjustSize(760,400);
	m_pUIContainer->LoadPage();
	m_iStopStat = 0;

	CButton *pBtn = (CButton*)GetDlgItem(IDC_RADIO_THIS_WEEK);
	pBtn->SetCheck(1);

	return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CMailStatDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CMailStatDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}


void CMailStatDlg::OnBnClickedBtnAbout()
{
	CAboutDlg dlg;
	dlg.DoModal();

}

void CMailStatDlg::OnBnClickedButtonClose()
{
	g_iStopStat = 1;
	DeInit();
	SendMessage(WM_CLOSE,0,0);
}

HBRUSH CMailStatDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
	HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

	if(nCtlColor == CTLCOLOR_STATIC)  
	{ 
		if (pWnd->GetDlgCtrlID() == IDC_STATIC_TITLELBAR)
		{
			pDC->SetTextColor(RGB(0,0,0));  
			pDC->SetBkColor(RGB(255,0,0)); 
			return m_hTitleBarBrush;
		}
		else
		{
			pDC->SetTextColor(RGB(0,0,0));  
			pDC->SetBkColor(DLG_BK_COLOR); 
		}
		return m_hDialogBrush;
	}
	else if (nCtlColor == CTLCOLOR_DLG)
	{
		//pDC->SetTextColor(COLOR_DLG_VC);
		return m_hDialogBrush;
	}
	return m_hDialogBrush;
}

BOOL CMailStatDlg::PtInClientRect(CPoint p)
{
	CRect rc;
	GetClientRect(&rc);
	ClientToScreen(&rc);
	return rc.PtInRect(p);
}


LRESULT CMailStatDlg::OnNcHitTest(CPoint point)
{
	return PtInClientRect(point) ? HTCAPTION : CDialog::OnNcHitTest(point);
}

void CMailStatDlg::OnBnClickedButtonMin()
{
	ShowWindow(SW_MINIMIZE);
}


void CMailStatDlg::OnBnClickedCheckTimeRange()
{
	CButton* pBtn = (CButton*)GetDlgItem(IDC_CHECK_TIME_RANGE);
	CButton* pBtnR1 = (CButton*)GetDlgItem(IDC_RADIO_WHOLE);
	CButton* pBtnR2 = (CButton*)GetDlgItem(IDC_RADIO_THIS_MONTH);
	CButton* pBtnR3 = (CButton*)GetDlgItem(IDC_RADIO_THIS_WEEK);
	CButton* pBtnR4 = (CButton*)GetDlgItem(IDC_RADIO_TODAY);

	if (pBtn->GetCheck())
	{
		GetDlgItem(IDC_STATIC_FROM)->ShowWindow(SW_SHOW);
		GetDlgItem(IDC_FROM_DATE)->ShowWindow(SW_SHOW);
		GetDlgItem(IDC_STATIC_TO)->ShowWindow(SW_SHOW);
		GetDlgItem(IDC_TO_DATE)->ShowWindow(SW_SHOW);
		pBtnR1->SetCheck(0);
		pBtnR2->SetCheck(0);
		pBtnR3->SetCheck(0);
		pBtnR4->SetCheck(0);
	}
	else
	{	GetDlgItem(IDC_STATIC_FROM)->ShowWindow(SW_HIDE);
	GetDlgItem(IDC_FROM_DATE)->ShowWindow(SW_HIDE);
	GetDlgItem(IDC_STATIC_TO)->ShowWindow(SW_HIDE);
	GetDlgItem(IDC_TO_DATE)->ShowWindow(SW_HIDE);
	}
}


int GetStatType(FILETIME &ftStartTime,FILETIME &ftEndTime)
{
	int iCmp = CompareFileTime(&ftStartTime,&ftEndTime);
	if (iCmp == PE_RET_CMP_GREATER)
	{
		return STAT_TYPE_UNKNOW;
	}

	int iDiff = GetFileTimeIntervalBySeconds(ftStartTime,ftEndTime);
	int iOneDay = 60 * 60 * 24;
	iDiff /= iOneDay;
	if (iDiff == 0)
	{
		//within a day
		return STAT_TYPE_BY_HOUR;
	}

	if (iDiff > 0 && iDiff < 60)
	{
		return STAT_TYPE_BY_DAY;
	}

	if (iDiff >= 60 && iDiff < 365 * 2)
	{
		return STAT_TYPE_BY_MONTH;
	}

	return STAT_TYPE_BY_YEAR;
}

int g_iExport = 0;
TCHAR szExportFileName[255];
void CMailStatDlg::OnBnClickedBtnExport()
{
	CString sFileName = _T("MailStat");

	TCHAR *szPrefix = _T("csv Files (*.csv)|*.csv||");

	CFileDialog dlg(false,_T("csv"),sFileName,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,szPrefix,NULL);
	if ( IDOK != dlg.DoModal())
	{
		return;
	}

	CString sPath = dlg.GetPathName();
	_tcscpy(szExportFileName,sPath.GetBuffer());
	sPath.ReleaseBuffer();

	g_iExport = 1;
	OnBnClickedBtnStartStat();
}


void CMailStatDlg::OnBnClickedBtnStartStat()
{
	m_pUIContainer->ShowWindow(SW_HIDE);

	if (m_iStopStat == 1)
	{
		m_btnStart.EnableWindow(FALSE);
		g_iStopStat = 1;
		return;
	}

	g_iStopStat = 0;

	int iStatType = STAT_TYPE_UNKNOW;
	FILETIME ftStartTime,ftEndTime;

	CButton* pBtn = (CButton*)GetDlgItem(IDC_CHECK_TIME_RANGE);
	if (pBtn->GetCheck())
	{
		SYSTEMTIME stStartTime,stEndTime;
		m_StartTimePicker.GetTime(&stStartTime);
		m_EndTimePicker.GetTime(&stEndTime);

		SystemTimeToFileTime(&stStartTime,&ftStartTime);
		SystemTimeToFileTime(&stEndTime,&ftEndTime);

		iStatType = GetStatType(ftStartTime,ftEndTime);
		if (iStatType == STAT_TYPE_UNKNOW)
		{
			MessageBox(_T("Time range seems incorrect"),_T("Error"),MB_ICONSTOP|MB_OK);
			return;
		}

		StartStatProcess(ftStartTime,ftEndTime,iStatType);
		m_iStopStat = 1;
		m_btnStart.SetWindowText(_T("Stop Analyse"));
		return;
	}

	pBtn = (CButton*)GetDlgItem(IDC_RADIO_WHOLE);
	if (pBtn->GetCheck())
	{
		memset(&ftStartTime,0,sizeof(FILETIME));
		memset(&ftEndTime,0,sizeof(FILETIME));
		iStatType = STAT_TYPE_BY_YEAR;
		m_iStopStat = 1;
		m_btnStart.SetWindowText(_T("Stop Analyse"));

		StartStatProcess(ftStartTime,ftEndTime,iStatType);
		return;
	}

	SYSTEMTIME stNow;
	GetLocalTime(&stNow);
	SystemTimeToFileTime(&stNow,&ftEndTime);
	ftStartTime = ftEndTime;
	pBtn = (CButton*)GetDlgItem(IDC_RADIO_THIS_MONTH);
	if (pBtn->GetCheck())
	{
		iStatType = STAT_TYPE_BY_DAY;
		time_t timeDiff = stNow.wDay * 24 * 60 * 60;
		timeDiff *= (ONE_SECOND * (-1));
		AddFileTimeSpan(ftStartTime,timeDiff);

		m_iStopStat = 1;
		m_btnStart.SetWindowText(_T("Stop Analyse"));
		StartStatProcess(ftStartTime,ftEndTime,iStatType);
		return;
	}

	pBtn = (CButton*)GetDlgItem(IDC_RADIO_THIS_WEEK);
	if (pBtn->GetCheck())
	{
		iStatType = STAT_TYPE_BY_DAY;
		time_t timeDiff = 7 * 24 * 60 * 60;
		timeDiff *= (ONE_SECOND * (-1));;
		AddFileTimeSpan(ftStartTime,timeDiff);

		SYSTEMTIME st1,st2;
		FileTimeToSystemTime(&ftStartTime,&st1);
		FileTimeToSystemTime(&ftEndTime,&st2);

		m_iStopStat = 1;
		m_btnStart.SetWindowText(_T("Stop Analyse"));
		StartStatProcess(ftStartTime,ftEndTime,iStatType);
		return;
	}

	pBtn = (CButton*)GetDlgItem(IDC_RADIO_TODAY);
	if (pBtn->GetCheck())
	{
		iStatType = STAT_TYPE_BY_HOUR;
		time_t timeDiff = stNow.wHour * 60 * 60;
		timeDiff *= (ONE_SECOND * (-1));;
		AddFileTimeSpan(ftStartTime,timeDiff);

		m_iStopStat = 1;
		m_btnStart.SetWindowText(_T("Stop Analyse"));
		StartStatProcess(ftStartTime,ftEndTime,iStatType);
		return;
	}

	MessageBox(_T("Please Select a analyse type first"),_T("Error"),MB_ICONSTOP|MB_OK);

}

void CMailStatDlg::SetProgress(int iTotal,int iCur)
{
	float fPercent = ((float)iCur/(float)iTotal) * 100;
	m_StatProgress.SetPos((int)fPercent);

	CString sInfo;
	if (m_iFolderType == MAIL_TYPE_SENT)
	{
		sInfo = _T("Now Analysing Sent Items:");
	}
	else
	{
		sInfo = _T("Now Analysing Inbox:");
	}

	m_StatProgress.SetWindowText(sInfo);
}

BOOL CMailStatDlg::OnWndMsg(UINT message, WPARAM wParam, LPARAM lParam, LRESULT* pResult)
{
	if (message == WM_STAT_DONE)
	{
		m_StatProgress.SetPos(100);
		m_StatProgress.SetWindowText(_T(""));

		m_btnStart.EnableWindow(TRUE);
		m_btnStart.SetWindowText(_T("Start Analyse"));
		m_iStopStat = 0;

		m_pUIContainer->ShowWindow(SW_SHOW);
		m_pUIContainer->LoadPage();

		g_iExport = 0;

		return TRUE;
	}

	if (message == WM_FOLDER_TYPE)
	{
		m_iFolderType = (int)wParam;
		return TRUE;
	}

	if (message == WM_STAT_PROGRESS)
	{
		SetProgress((int)wParam,(int)lParam);
		return TRUE;
	}

	return CDialog::OnWndMsg(message, wParam, lParam, pResult);
}

void CMailStatDlg::OnBnClickedRadioWhole()
{
	CButton* pBtn = (CButton*)GetDlgItem(IDC_CHECK_TIME_RANGE);
	pBtn->SetCheck(0);
	GetDlgItem(IDC_STATIC_FROM)->ShowWindow(SW_HIDE);
	GetDlgItem(IDC_FROM_DATE)->ShowWindow(SW_HIDE);
	GetDlgItem(IDC_STATIC_TO)->ShowWindow(SW_HIDE);
	GetDlgItem(IDC_TO_DATE)->ShowWindow(SW_HIDE);
}

void CMailStatDlg::OnBnClickedRadioThisMonth()
{
	CButton* pBtn = (CButton*)GetDlgItem(IDC_CHECK_TIME_RANGE);
	pBtn->SetCheck(0);
	GetDlgItem(IDC_STATIC_FROM)->ShowWindow(SW_HIDE);
	GetDlgItem(IDC_FROM_DATE)->ShowWindow(SW_HIDE);
	GetDlgItem(IDC_STATIC_TO)->ShowWindow(SW_HIDE);
	GetDlgItem(IDC_TO_DATE)->ShowWindow(SW_HIDE);
}

void CMailStatDlg::OnBnClickedRadioThisWeek()
{
	CButton* pBtn = (CButton*)GetDlgItem(IDC_CHECK_TIME_RANGE);
	pBtn->SetCheck(0);
	GetDlgItem(IDC_STATIC_FROM)->ShowWindow(SW_HIDE);
	GetDlgItem(IDC_FROM_DATE)->ShowWindow(SW_HIDE);
	GetDlgItem(IDC_STATIC_TO)->ShowWindow(SW_HIDE);
	GetDlgItem(IDC_TO_DATE)->ShowWindow(SW_HIDE);
}

void CMailStatDlg::OnBnClickedRadioToday()
{
	CButton* pBtn = (CButton*)GetDlgItem(IDC_CHECK_TIME_RANGE);
	pBtn->SetCheck(0);
	GetDlgItem(IDC_STATIC_FROM)->ShowWindow(SW_HIDE);
	GetDlgItem(IDC_FROM_DATE)->ShowWindow(SW_HIDE);
	GetDlgItem(IDC_STATIC_TO)->ShowWindow(SW_HIDE);
	GetDlgItem(IDC_TO_DATE)->ShowWindow(SW_HIDE);
}


